package cn.b.sky.mes.em.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.em.model.MaintainStrategy;
import cn.b.sky.mes.em.model.condition.MaintainStrategyCondition;
import cn.b.sky.mes.em.service.MaintainStrategyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.cm.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.model.ECode;

@Controller
@RequestMapping(value = "/maintainStrategy/*")
public class MaintainStrategyController extends SecurityController {
  @Resource
  MaintainStrategyService maintainStrategyService;

  @Resource
  private ECodeService eCodeService;

  @RequestMapping("strategyList")
  public String strategyList(Model model, @ModelAttribute("condition") MaintainStrategyCondition strategy,
      String navTabId) {
    strategy.setSiteCode(SecurityContextUtil.getSiteCode());
    if (StringUtils.isBlank(strategy.getOrderField())) {
      strategy.setOrderField("createDateTime");
    }
    strategy.setLanguageCode(SecurityContextUtil.getLanguageCode());
    strategy.setStrategyTypeECodeType(ECodeEnums.MAINTAINSTRATEGYTYPE.getCode());
    strategy.setTimeUnitECodeType(ECodeEnums.TIMEUNITECODE.getCode());
    List<MaintainStrategy> strategyList = maintainStrategyService.selectMaintainStrategyByCondition(strategy);
    int count = maintainStrategyService.selectMaintainStrategyCount(strategy);
    strategy.setData(strategyList);
    strategy.setTotalCount(count);
    List<ECode> strategyType = eCodeService.selectECodeList(ECodeEnums.MAINTAINSTRATEGYTYPE.getCode(),
        SecurityContextUtil.getLanguageCode());
    List<ECode> timeUnit = eCodeService.selectECodeList(ECodeEnums.TIMEUNITECODE.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("strategyType", strategyType);
    model.addAttribute("timeUnit", timeUnit);
    model.addAttribute("page", strategy);
    model.addAttribute(NAVTABID, navTabId);
    return "em/maintainStrategy/maintainStrategy_list";
  }

  @RequestMapping("addStrategy")
  public String addStrategy(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    List<ECode> strategyType = eCodeService.selectECodeList(ECodeEnums.MAINTAINSTRATEGYTYPE.getCode(),
        SecurityContextUtil.getLanguageCode());
    List<ECode> timeUnit = eCodeService.selectECodeList(ECodeEnums.TIMEUNITECODE.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("strategyType", strategyType);
    model.addAttribute("timeUnit", timeUnit);
    return "em/maintainStrategy/maintainStrategy_add";
  }

  @RequestMapping("modifyStrategy")
  public String modifyStrategy(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    List<ECode> strategyType = eCodeService.selectECodeList(ECodeEnums.MAINTAINSTRATEGYTYPE.getCode(),
        SecurityContextUtil.getLanguageCode());
    List<ECode> timeUnit = eCodeService.selectECodeList(ECodeEnums.TIMEUNITECODE.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("strategyType", strategyType);
    model.addAttribute("timeUnit", timeUnit);
    MaintainStrategy strategy = maintainStrategyService.selectMaintainStrategyByCode(getSiteCode(), id);
    model.addAttribute("strategy", strategy);
    return "em/maintainStrategy/maintainStrategy_add";
  }

  @RequestMapping("validateStrategyCode")
  @ResponseBody
  public boolean validateStrategyCode(String strategyCode, Model model) {
    if (!StringUtils.isBlank(strategyCode)) {
      MaintainStrategy strategy = maintainStrategyService.selectMaintainStrategyByCode(getSiteCode(), strategyCode);
      if (strategy != null && !StringUtils.isBlank(strategy.getStrategyCode())) {
        return false;
      }
    }
    return true;
  }

  @RequestMapping("saveStrategy")
  @ResponseBody
  public DWZResult saveStrategy(Model model, String navTabId, MaintainStrategy strategy, String id,
      HttpServletRequest request) {
    if (strategy == null) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    strategy.setSiteCode(getSiteCode());
    setCreateInfo(strategy);
    if (StringUtils.isBlank(id)) {
      maintainStrategyService.insertMaintainStrategy(strategy);
      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null, null, null, null);
    } else {
      maintainStrategyService.updateMaintainStrategy(strategy);
      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null, null, null, null);

    }
  }

  @RequestMapping("viewStrategy")
  public String viewStrategy(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    List<ECode> strategyType = eCodeService.selectECodeList(ECodeEnums.MAINTAINSTRATEGYTYPE.getCode(),
        SecurityContextUtil.getLanguageCode());
    List<ECode> timeUnit = eCodeService.selectECodeList(ECodeEnums.TIMEUNITECODE.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("strategyType", strategyType);
    model.addAttribute("timeUnit", timeUnit);
    MaintainStrategy strategy = maintainStrategyService.selectMaintainStrategyByCode(getSiteCode(), id);
    model.addAttribute("strategy", strategy);
    return "em/maintainStrategy/maintainStrategy_view";
  }

  @RequestMapping("deleteStrategy")
  @ResponseBody
  public DWZResult deleteStrategy(Model model, String navTabId, String id, HttpServletRequest request) {
    if (StringUtils.isBlank(id)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    String ids = request.getParameter("ids");
    String[] ress = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(ress.length);
    boolean hasFail = false;
    for (String res : ress) {
      BatchJobReport report = maintainStrategyService.deleteMaintainStrategy(getSiteCode(), res);
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      // 直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, reports);
    }

  }

  @RequestMapping("strategySuggest")
  @ResponseBody
  public Object strategySuggest(@ModelAttribute("condition") MaintainStrategyCondition condition) {
    condition.setSiteCode(this.getSiteCode());
    condition.setLockFlag(0);
    condition.setLanguageCode(SecurityContextUtil.getLanguageCode());
    condition.setStrategyTypeECodeType(ECodeEnums.MAINTAINSTRATEGYTYPE.getCode());
    condition.setTimeUnitECodeType(ECodeEnums.TIMEUNITECODE.getCode());
    return maintainStrategyService.selectMaintainStrategyForSuggest(condition);
  }

  @RequestMapping("validateStrategyCodeForSelect")
  @ResponseBody
  public boolean validateStrategyCodeForSelect(String strategyCode, Model model) {
    if (!StringUtils.isBlank(strategyCode)) {
      MaintainStrategy strategy = maintainStrategyService.selectMaintainStrategyByCode(getSiteCode(), strategyCode);
      if (strategy != null && strategy.getLockFlag() == 0) {
        return true;
      }
    }
    return false;
  }
}
